import { getGoodsList } from '../../service/goods'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    param:{},
    categories: [],
    mainActiveIndex: 0,
    activeId: null,
    goods: [],
    page: 1,
    isLast: false,
    isLoading: false,
    value1: 0,
    option1: [
      { text: '默认', value: 0 },
      { text: '销量(由高到低)', value: 1 },
      { text: '价格(由高到低)', value: 2 },
      { text: '评论数(由高到低)', value: 3 },
    ],
    value: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    getGoodsList().then(res => {
      console.log(res)
      this.getCategories(res.categories)
      const param = {page: this.data.page }
      this.setData({param})
      this.getData(param)
    })
  },
  onClickNav(e) {
    this.setData({
      mainActiveIndex: e.detail.index || 0,
    });
  },
  onClickItem(e) {
    this.selectComponent('#item').toggle();
    const activeId = e.detail.id;
    this.setData({ activeId, goods: [], page: 1,isLast:false });
    const category_id = this.data.activeId
    const param = { category_id, page: this.data.page }
    this.setData({param})
    this.getData(param)
  },

  getData(param) {
    this.setData({
      isLoading: true
    })
    getGoodsList(param).then(res => {
      console.log(res)
      this.setData({ goods: [...this.data.goods, ...res.goods.data], isLoading: false })

      // 判断是否最后一页
      console.log(res.goods.data.length)
      console.log(res.goods.per_page)
      if (res.goods.data.length < res.goods.per_page) {
        this.setData({ isLast: true })
      }
    })
  },

  getCategories(res) {
    /**第一种方法,循环往新数组添加数据 */
    // let categories = []
    // let category = res.map(item => {
    //   let data = { text: '', children: [] }
    //   data.text = item.name
    //   item.children.map(item2 => {
    //     let children = {
    //       text: '',
    //       id: 1,
    //       pid: ''
    //     }
    //     children.text = item2.name
    //     children.id = item2.id
    //     children.pid = item2.pid
    //     data.children.push(children)

    //   })
    //   categories.push(data)
    // })
    // console.log(categories)
    // this.setData({ categories })

    /**第二种方法 正则找出并替换(把取到的数据字段name变成text) */
    let categories=JSON.stringify(res)
    categories=categories.replace(/name/gi,'text')
    categories=JSON.parse(categories)
    this.setData({categories})

  },

  /**
* 触底加载更多
//  */
  onReachBottom() {
    console.log(this.data.param)
    console.log(this.data.isLast)
    if (this.data.isLast) return
    console.log(this.data.param)
    this.setData({ page: this.data.page + 1})
    this.data.param.page=this.data.page
    this.setData({param:this.data.param})
    
    this.getData(this.data.param)
  },

  /**
   * 用户输入触发
   */
  onChange(e) {
    this.setData({ value: e.detail })
  },


  /**
   * 用户搜索
   */
    inputSearch(){
      this.setData({ goods: [], page: 1,isLast:false });
      if(this.data.activeId){
        var param = { title:this.data.value, page: this.data.page,category_id:this.data.activeId }
      }else{
        var param = { title:this.data.value, page: this.data.page}
      }

      this.setData({param})
      this.getData(this.data.param)
    },
  /**
   * 点击搜索按钮
   */
  onClickSearch() {
    this.inputSearch()
  },
  /**
   * 输入后按下回车
   */
   onSearch(){
    this.inputSearch()
   },

   /**
    * 清空搜索框
    */
    clearInput(){
      this.getData()
    },

   /**
    * 改变排序
    */
    changeSort(){
      console.log(12)
    }


})